//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Callability.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Event.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCallability::CCallability (boost::shared_ptr<QuantLib::Callability>& childNative, Object^ owner) : CEvent(CCallability::typeid)
{
#ifdef HANDLE
	_phCallability = NULL;
#endif
	_ppCallability = &childNative;
    _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
}
Cephei::QL::Instruments::CCallability::CCallability (QuantLib::Callability& childNative, Object^ owner) : CEvent(CCallability::typeid)
{
#ifdef HANDLE
	_phCallability = NULL;
#endif
	_ppCallability = new boost::shared_ptr<QuantLib::Callability> (&childNative);
    _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
    _CallabilityOwner = owner;
    _EventOwner = owner;
}

Cephei::QL::Instruments::CCallability::CCallability (CCallability^ copy) : CEvent(CCallability::typeid)
{
#ifdef HANDLE
	_phCallability = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallability = new boost::shared_ptr<QuantLib::Callability> (copy->GetShared());
        _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
    }
}
Cephei::QL::Instruments::CCallability::CCallability (System::Type^ t) : CEvent(CCallability::typeid)
{
#ifdef HANDLE
	_phCallability = NULL;
#endif
	if (!t->IsSubclassOf(CCallability::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCallability::CCallability (QuantLib::Handle<QuantLib::Callability>& childNative, Object^ owner)  : CEvent(CCallability::typeid)
{
	_phCallability = &childNative;
	_ppCallability = &static_cast<boost::shared_ptr<QuantLib::Callability>>(childNative.currentLink());
    _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
    _CallabilityOwner = owner;
}
Cephei::QL::Instruments::CCallability::CCallability (QuantLib::Handle<QuantLib::Callability> childNative)  : CEvent(CCallability::typeid)
{
	_phCallability = &childNative;
	_ppCallability = &static_cast<boost::shared_ptr<QuantLib::Callability>>(childNative.currentLink());
    _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCallability::CCallability (QuantLib::Callability childNative)  : CEvent(CCallability::typeid)
{
#ifdef HANDLE
	_phCallability = NULL;
#endif
	_ppCallability = new boost::shared_ptr<QuantLib::Callability> (new QuantLib::Callability (childNative));
    _ppEvent = new boost::shared_ptr<QuantLib::Event> (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
}
#endif

Cephei::QL::Instruments::CCallability::~CCallability ()
{
    if (_ppCallability != NULL)
    {
	    delete _ppCallability;
        _ppCallability = NULL;
    }
}
Cephei::QL::Instruments::CCallability::!CCallability ()
{
    if (_ppCallability != NULL)
    {
	    delete _ppCallability;
    }
}
QuantLib::Callability& Cephei::QL::Instruments::CCallability::GetReference ()
{
    if (_ppCallability == NULL) throw gcnew NativeNullException ();
	return **_ppCallability;
}
boost::shared_ptr<QuantLib::Callability>& Cephei::QL::Instruments::CCallability::GetShared ()
{
    if (_ppCallability == NULL) throw gcnew NativeNullException ();
	return *_ppCallability;
}
QuantLib::Callability* Cephei::QL::Instruments::CCallability::GetPointer ()
{
    if (_ppCallability == NULL) throw gcnew NativeNullException ();
	return &**_ppCallability;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Callability>& Cephei::QL::Instruments::CCallability::GetHandle ()
{
	if (_phCallability == NULL)
	{
		_phCallability = new Handle<QuantLib::Callability> (*_ppCallability);
	}
	return *_phCallability;
}
#endif
bool Cephei::QL::Instruments::CCallability::HasNative () 
{
	return (_ppCallability != NULL);
}

DateTime Cephei::QL::Instruments::CCallability::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCallability)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Instruments::Callability::TypeEnum Cephei::QL::Instruments::CCallability::Type::get ()
{
    try
    {
    	QuantLib::Callability::Type _rv = (QuantLib::Callability::Type)(*_ppCallability)->type ( );   
        QL::Instruments::Callability::TypeEnum _nrv = (QL::Instruments::Callability::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CCallabilityVector::CCallabilityVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Instruments::CCallabilityVector::~CCallabilityVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Instruments::CCallabilityVector::!CCallabilityVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Instruments::CCallabilityVector::CCallabilityVector (std::vector<QuantLib::Callability>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Instruments::CCallabilityVector::CCallabilityVector (std::vector<QuantLib::Callability> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::Callability> (vec);
}
#endif
Cephei::QL::Instruments::CCallabilityVector::CCallabilityVector (std::vector<boost::shared_ptr<QuantLib::Callability>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Instruments::CCallabilityVector::CCallabilityVector (std::vector<QuantLib::Handle<QuantLib::Callability>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Instruments::CCallabilityVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Instruments::ICallability^ Cephei::QL::Instruments::CCallabilityVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw gcnew RangeException ();
		QuantLib::Callability& native = (*_pVValue)[index];
		return gcnew CCallability (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw gcnew RangeException ();
		QuantLib::Callability& native = *(*_pVBoost)[index];
		return gcnew CCallability (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw gcnew RangeException ();
		QuantLib::Callability& native = *((*_pVHand)[index].currentLink ());
		return gcnew CCallability (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Instruments::CCallabilityVector::default::set (int index, Cephei::QL::Instruments::ICallability^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CCallability^>(value)->GetReference ());
		}
        else
        {
            QuantLib::Callability& c = (*_pVValue)[index];
            c = dynamic_cast<CCallability^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CCallability^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::Callability>& c = (*_pVBoost)[index];
		c = dynamic_cast<CCallability^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::Callability> ());
		}
		QuantLib::Handle<QuantLib::Callability>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::Callability>& b = static_cast<boost::shared_ptr<QuantLib::Callability>>(c.currentLink());
		b = dynamic_cast<CCallability^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^ Cephei::QL::Instruments::CCallabilityVector::CItem::get (int index) 
{
	return CellBuilder::Instance->Create<ICallability^> (dynamic_cast<ICallability^>(default[index]));
}

void Cephei::QL::Instruments::CCallabilityVector::CItem::set (int index, Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^ value) 
{
	default[index] = safe_cast<ICallability^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::Callability>& Cephei::QL::Instruments::CCallabilityVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::Callability>>& Cephei::QL::Instruments::CCallabilityVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::Callability>>& Cephei::QL::Instruments::CCallabilityVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Instruments::CCallabilityVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::Callability> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::Callability>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::Callability>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of Callability that can be passed into other function </summary> */
IVector<Cephei::QL::Instruments::ICallability^>^ Cephei::QL::Instruments::CCallability_Factory::CreateVector ()
 {
	 return gcnew CoVector<ICallability^> (gcnew CCallabilityVector ());
 }
IVector<Cephei::QL::Instruments::ICallability^>^ Cephei::QL::Instruments::CCallability_Factory::CreateVector (System::Collections::Generic::IEnumerable<ICallability^>^ source)
 {
	 return gcnew CoVector<ICallability^> (source, gcnew CCallabilityVector ());
 }
/* <summary>Create a single dimension array of Callability with change events</summary> */
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^>^>^ Cephei::QL::Instruments::CCallability_Factory::CreateCellVector ()
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^> (gcnew CCallabilityVector ()));
 }
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^>^>^ Cephei::QL::Instruments::CCallability_Factory::CreateCellVector (System::Collections::Generic::IEnumerable<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^>^ source)
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Instruments::ICallability^>^> (source, gcnew CCallabilityVector ()));
 }

